package com.itheima.controller;

import com.itheima.domain.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * ClassName:PermissionController
 * Package:com.itheima.controller
 * Description:
 * Date:2020/3/12 15:38
 * User: snow
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {

	@Autowired
	private PermissionService permissionService;

	@RequestMapping("/findAll.do")
	public ModelAndView findAll() throws Exception {
		ModelAndView view = new ModelAndView();
		List<Permission> permissions = permissionService.findAll();
		view.addObject("permissionList", permissions);

		view.setViewName("permission-list");
		return view;
	}

	/**
	 * 添加权限
	 * @param permission
	 * @return
	 * @throws Exception
	 */

	@RequestMapping("/save.do")
	public String save(Permission permission) throws Exception {
		permissionService.save(permission);
		return "redirect:findAll.do";
	}

	@RequestMapping("/findById.do")
	public ModelAndView findById(String id) throws Exception {
		ModelAndView view = new ModelAndView();
		Permission permission = permissionService.findById(id);
		view.addObject("permission", permission);
		view.setViewName("permission-show");
		return view;
	}


}
